package com.example.demo;

import com.example.demo.mapper.QuestionBankMapper;
import com.example.demo.mapper.QuestionCategoryMapper;
import com.example.demo.mapper.QuestionClassificationMapper;
import com.example.demo.mapper.QuestionLabelMapper;
import com.example.demo.pojo.Unit.QuestionNumber;
import com.example.demo.pojo.entity.QuestionClassification;
import com.example.demo.pojo.entity.QuestionLabel;
import com.example.demo.pojo.vo.LabelCountVO;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

@SpringBootTest
@RequiredArgsConstructor
public class createJuanzi {
    //全局变量
    List<QuestionClassification>questionClassifications = new ArrayList<>();
    @Autowired
    private QuestionBankMapper questionBankMapper;
    @Autowired
    private QuestionCategoryMapper questionCategoryMapper;
    @Autowired
    private QuestionClassificationMapper questionClassificationMapper;
    @Autowired
    private QuestionLabelMapper questionLabelMapper;
    //获取题目数据
    @Test
    void getNumber(){
        List<QuestionLabel>questionLabelList=questionLabelMapper.initQuestionLabel(1);
        List<QuestionClassification>questionClassificationListRoot=questionClassificationMapper.groundQuestionClassificationByRootId(1);
        List<LabelCountVO>labelCountVOList=new ArrayList<>();
        for(int i=0;i<questionLabelList.size();i++){
            QuestionLabel questionLabel=questionLabelList.get(i);
            String nodeId=questionLabel.getNodeId();
            List<QuestionClassification> questionClassificationList=questionClassificationMapper.getQuestionClassificationByNodeId(nodeId);
            int number=questionClassificationList.size();
            LabelCountVO labelCountVO=new LabelCountVO();
            labelCountVO.initLabel(questionLabel);
            if(questionLabel.getId()==1){
                labelCountVO.setCount(questionClassificationListRoot.size());
            }else{
                labelCountVO.setCount(number);
            }
            labelCountVOList.add(labelCountVO);
        }
        //验证
        for(int i=0;i<labelCountVOList.size();i++){
            System.out.println(labelCountVOList.get(i));
        }
    }
    //根据题型查找相关题目--抽取虚题目
    List<QuestionClassification> getImageQuestionClassificationsByLabel(String nodeId,int number){
        //模拟数据--传入参数为题型id
        List<QuestionClassification>questionClassificationList=questionClassificationMapper.getQuestionClassificationByNodeId(nodeId);
        System.out.println("题型查找相关题目");
        List<QuestionClassification>copyList=new ArrayList<>(questionClassificationList);
        Collections.shuffle(copyList);
        for(int i=0;i<questionClassificationList.size();i++){
            System.out.println(questionClassificationList.get(i));
        }
        return copyList.subList(0,number);
    }
    //根据题型查找相关题目--抽取实题目
    List<QuestionClassification> getRealQuestionClassificationsByLabel(int labelId,int number){
        List<QuestionClassification>questionClassificationList=questionClassificationMapper.getQuestionClassificationByLabelId(labelId);
        System.out.println("题型查找相关题目");
        List<QuestionClassification>copyList=new ArrayList<>(questionClassificationList);
        Collections.shuffle(copyList);
        return copyList.subList(0,number);
    }
    //根据nodeId查询链条所有支点
    List<QuestionLabel>initQuestionLabelLink(String nodeId){
        List<QuestionLabel>questionLabelList=new ArrayList<>();
        String[] ids=nodeId.split("-");
        for(int i=0;i<ids.length;i++){
            String temp=ids[i];
            QuestionLabel questionLabel=questionLabelMapper.selectById(Integer.parseInt(temp));
            questionLabelList.add(questionLabel);
        }
        //测试
//        System.out.println("支线查询节点");
//        for(int i=0;i<questionLabelList.size();i++){
//            System.out.println(questionLabelList.get(i));
//        }
        return questionLabelList;
    }
    //递归将数目平分
    void divide(int number, int parts, int currentSum, List<Integer> result) {
        if (parts == 0) {
            if (number == 0) {
                System.out.println(result);
            }
            return;
        }
        for (int i = 1; i <= number; i++) {
            result.add(i);
            divide(number - i, parts - 1, currentSum + i, result);
            result.remove(result.size() - 1);
        }
    }
    //链条数目减少
    void linkNumberReduce(HashMap<QuestionLabel,Integer>testDTO,List<QuestionLabel>questionLabelLink,int number){
        if(questionLabelLink!=null){
            for(int i=0;i<questionLabelLink.size();i++){
                int oldValue=testDTO.get(questionLabelLink.get(i));
                int newValue=oldValue-number;
                testDTO.put(questionLabelLink.get(i),newValue);
            }
        }
    }
    //处理题型和数目间的关希---采用递归的关系
    void solveClassification(HashMap<QuestionLabel,Integer>testDTO,QuestionLabel questionLabel,int number){
        //设置递归结束条件
        if(number==0){
            return;
        }
        List<Integer>realBankIdList=questionClassificationMapper.initQuestionClassificationByLabelId(questionLabel.getId());
        List<Integer>imageBankIdList=questionClassificationMapper.initQuestionClassificationByLabelNodeId(questionLabel.getNodeId());
        int realNumber=realBankIdList.size();
        int imageNumber=imageBankIdList.size();
        int needNumber=testDTO.get(questionLabel);
        System.out.println("数据检验---处理");
        System.out.println("真实题目"+realNumber);
        System.out.println("需要题目"+needNumber);
        System.out.println("虚假题目"+imageNumber);
        System.out.println("抽取数量"+testDTO.get(questionLabel));

        //如果真实题目大于需求题目---进行真实题目抽取(优先)
        if(realNumber>=needNumber){
            List<QuestionClassification>realClassificationList=getRealQuestionClassificationsByLabel(questionLabel.getId(),testDTO.get(questionLabel));
            questionClassifications.addAll(realClassificationList);
            int listNumber=realClassificationList.size();
            //处理链条数目
            List<QuestionLabel>questionLabelLink=initQuestionLabelLink(questionLabel.getNodeId());
            linkNumberReduce(testDTO,questionLabelLink,listNumber);
            //处理其他节点信息
            List<QuestionClassification>relevanceQuestionClassificationList=getOtherQuestionClassification(realClassificationList);
            if(relevanceQuestionClassificationList!=null){
                for(int j=0;j<relevanceQuestionClassificationList.size();j++){

                }
            }
        }
        //如果真实题目不足---先满足真实题目抽出，剩下进行随机分配
        else{
            int remainNumber=needNumber-realNumber;
            if(remainNumber!=0){
                List<QuestionClassification>realClassificationList=getRealQuestionClassificationsByLabel(questionLabel.getId(),testDTO.get(questionLabel));
                List<QuestionLabel>questionLabelLink=initQuestionLabelLink(questionLabel.getNodeId());
                linkNumberReduce(testDTO,questionLabelLink,realClassificationList.size());
                //处理其他节点信息
                List<QuestionClassification>relevanceQuestionClassificationList=getOtherQuestionClassification(realClassificationList);
                if(relevanceQuestionClassificationList!=null){
                    for(int j=0;j<relevanceQuestionClassificationList.size();j++){

                    }
                }
                //添加全局变量
                questionClassifications.addAll(realClassificationList);
            }
            //分配权重--递归实现
            List<Integer>weightList=new ArrayList<Integer>();
            List<QuestionLabel>questionLabelList=questionLabelMapper.searchChildrenByNodeId(questionLabel.getNodeId(),questionLabel.getLevel()+1);
            if(questionLabelList!=null){
                int childrenNumber=questionLabelList.size();
                divide(remainNumber,childrenNumber,0,weightList);
                //对子节点进行递归处理
                for(int i=0;i<questionLabelList.size();i++){
                    int newNumber=weightList.get(i);
                    QuestionLabel newQuestionLabel=questionLabelList.get(i);
                    solveClassification(testDTO,newQuestionLabel,newNumber);
                }
            }
        }
    }
    //获取具有多种题型的题目,且题型间是在不同分支下
    List<QuestionClassification> getOtherQuestionClassification(List<QuestionClassification>questionClassificationList){
        List<QuestionClassification>resultList=new ArrayList<>();
        //获取与这个题目的其他关联组
        if(questionClassificationList!=null){
            for(int i=0;i<questionClassificationList.size();i++){
                int bankId=questionClassificationList.get(i).getQuestionBankId();
                String nodeId=questionClassificationList.get(i).getNodeId();
                List<QuestionClassification>realList=new ArrayList<>();
                List<QuestionClassification>questionClassifications=questionClassificationMapper.searchQuestionClassificationByBankId(bankId);
                if(questionClassifications!=null&&questionClassifications.size()>1) {
                    //从关联组中过滤同一支链
                    for(int j=0;j<questionClassifications.size();j++) {
                        QuestionLabel questionLabel1 = questionLabelMapper.selectById(questionClassificationList.get(i).getQuestionLabelId());
                        QuestionLabel questionLabel2 = questionLabelMapper.selectById(questionClassifications.get(j).getQuestionLabelId());
                        if(isInLine(questionLabel1.getNodeId(),questionLabel1.getLevel(),questionLabel2.getNodeId(),questionLabel2.getLevel())){
                            realList.add(questionClassifications.get(j));
                        }
                    }
                    //经过过滤后存在不在同一支点下的
                    if(realList.size()>0){
                        resultList.add(questionClassificationList.get(i));
                    }
                }
            }
        }
        //验证
        System.out.println("----------具有多种题型的题目------");
        for(int i=0;i<resultList.size();i++) {
            System.out.println(resultList.get(i));
        }
        return resultList;
    }
    //根据2个nodeId判断是否属于同一链上
    boolean isInLine(String nodeId1,int level1,String nodeId2,int level2){
        if(level1>=level2){
            if(nodeId1.contains(nodeId2)){
                return true;
            }
        }else{
            if(nodeId2.contains(nodeId1)){
                return true;
            }
        }
        return false;
    }
    //对其他节点的题目进行减处理
    void solveOtherLinkNumber(HashMap<QuestionLabel,Integer>testDTO,List<QuestionClassification>questionClassificationList,QuestionLabel questionLabel,int number){
        for(int i=0;i<questionClassificationList.size();i++){
            QuestionClassification questionClassification=questionClassificationList.get(i);
            List<QuestionClassification>relevanceList=questionClassificationMapper.searchQuestionClassificationByBankId(questionClassification.getQuestionBankId());
            for(int j=0;j<relevanceList.size();j++){
                QuestionClassification solveQuestionClassification=relevanceList.get(j);
                int labelId=solveQuestionClassification.getQuestionLabelId();
                if(labelId==questionLabel.getId()){
                    continue;
                }else{
                    //与当前的题型具有不同分支的题型
                    String otherNodeId=solveQuestionClassification.getNodeId();
                    QuestionLabel otherQuestionLabel=questionLabelMapper.selectById(solveQuestionClassification.getQuestionLabelId());
                    //如果存在支链需求
                    if(testDTO.containsKey(otherQuestionLabel)){
                        int realNumber;
                        int imageNumber;
                    }
                }
            }
        }
    }
    //题目测试
    @Test
    void solveNumberB(){
        //模拟数据---一个类似树形的数据
        HashMap<QuestionLabel,Integer>testDTO=new HashMap<>();
        QuestionLabel temp;
        temp=questionLabelMapper.selectById(2);
        testDTO.put(temp,30);
        temp=questionLabelMapper.selectById(5);
        testDTO.put(temp,10);
        temp=questionLabelMapper.selectById(6);
        testDTO.put(temp,10);
        temp=questionLabelMapper.selectById(1);
        testDTO.put(temp,30);
        //开始处理
        List<QuestionLabel>questionLabelListSolve=new ArrayList<>();
        for (Map.Entry<QuestionLabel, Integer> entry : testDTO.entrySet()) {
            QuestionLabel questionLabel=entry.getKey();
            questionLabelListSolve.add(questionLabel);
        }
        //对level进行处理--高到低进行排序
        Collections.sort(questionLabelListSolve, new Comparator<QuestionLabel>() {
            @Override
            public int compare(QuestionLabel o1, QuestionLabel o2) {
                return o2.getLevel()-o1.getLevel();
            }
        });
        //从低级level开始处理
        for(int i=0;i<questionLabelListSolve.size();i++){
            QuestionLabel questionLabel =questionLabelListSolve.get(i);
            //获取随机选取的有关题型
            System.out.println("-----------------------new------------------");
            System.out.println("处理的题型");
            System.out.println(questionLabel);
            //使用函数进行处理
            solveClassification(testDTO,questionLabelListSolve.get(i),testDTO.get(questionLabelListSolve.get(i)));
        }
        //验证
        for(int i=0;i<questionClassifications.size();i++){
            System.out.println(questionClassifications.get(i));
        }
    }
    //数据检验
    @Test
    void test(){
//        List<QuestionClassification>temp=questionClassificationMapper.
//        System.out.println(isInLine("1-2-3-",2,"1-2-6-4-",3));
//        List<QuestionClassification>temp=questionClassificationMapper.searchQuestionClassificationByBankId(11996);
//        for(int i=0; i<temp.size(); i++){
//            System.out.println(temp.get(i));
//        }
        System.out.println(questionClassificationMapper.initQuestionClassifications().size());
    }
    @Test
    void  createCategory(){

    }
}
